POV-Ray : Newsgroups : povray.advanced-users : Array irritation. : Re: Array irritation. Server Time
30 Jul 2024 04:19:35 EDT (-0400)
  Re: Array irritation.  
From: Warp
Date: 18 Oct 2000 07:21:52
Message: <39ed87d0@news.povray.org>
Pabs <pab### [at] hotmailcom> wrote:
: Maybe they thought that the compiler would insert instructions to convert an
: int to a double at runtime, unlikely though it is?

  Current compilers can do lots of optimizations. Some of them are even
surprising.
  I was once debugging this kind of code (in C++):

int main()
{
    int i = 1;
    cout << i << endl;
}

  It was very strange since the debugger didn't execute the 'int i=1;' line
at all and it didn't even show the value of the variable 'i'.
  I thought that "what the **** is wrong with this?" until I realized that
I had all compile optimizations turned on. I turned them off and then the
debugging worked as expected.

  What happened was that the optimizations made by the compiler had converted
the above code to one equivalent to this:

int main()
{
    cout << 1 << endl;
}

  The compiler had completely wiped out the 'i' variable and that's why the
debugging looked so strange :)

  Btw, too many times I see code like this:

a = var>>2;  // equivalent to: a = var/4;

  The idea is that shifting is usually a lot faster than dividing so people
optimize by hand in this way.
  This, however, is nowadays made in vain and makes only the code harder to
read. I'd say that every current C(++) compiler can internally optimize this:

a = var/4;

to this:

a = var>>2;

so it's not necessary to do it by hand.

  Sometimes the optimizations made by a compiler are just marvelous. For
example most implementations of gcc convert this kind of code:

(var << 5)|(var >> 27);

(supposing that 'var' is an unsigned int of size 32 bits)
to this (in assembler):

rol [Variable], 5

(ie. "rotate the value [Variable] to the left 5 positions").

  In C there's no operation to rotate values like in most assemblers so you
have to do it with two shifts and one or.
  It's just incredible how the compiler can figure out that what you are
actually trying to do is a rotation and compiles the rather complex command
into one rotation assembler instruction (instead of two shifts and an or).

-- 
main(i,_){for(_?--i,main(i+2,"FhhQHFIJD|FQTITFN]zRFHhhTBFHhhTBFysdB"[i]
):_;i&&_>1;printf("%s",_-70?_&1?"[]":" ":(_=0,"\n")),_/=2);} /*- Warp -*/


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.